ship=initArray2(32,0)

shipt=initArray2(32,1)

addhook("movetile","addimage")
function addimage(id,x,y)
	if (not pstack.inMShip[id]) then
		if (tile(x,y,"frame")==0 and ship[id]==0 and not pstack.docked[id]) then
			ship[id]=image("gfx/space/"..ships[shipt[id]].name..".png",1,0,200+id)
			parse("setarmor "..id.." 206")
		elseif (tile(x,y,"frame")~=0 and ship[id]~=0) then
			if (pstack.docked[id]) then
				parse("speedmod "..id.." "..ships[shipt[id]].smod)
				pstack.docked[id]=false
				table.remove(pstack.pdocked[fromUsgn(parties[inParty[id][2]].mship)],table.find(pstack.pdocked[fromUsgn(parties[inParty[id][2]].mship)],id))
			end
			freeimage(ship[id])
			ship[id]=0
			parse("setarmor "..id.." 0")
		end
	else
		if (tile(x,y,"frame")==0) then
			if (parties[inParty[id][2]].mshipi==0) then
				parties[inParty[id][2]].mshipi=image("gfx/space/"..ships["ms"].name..".png",1,0,200+id)
				parse("setarmor "..id.." 206")
			end
			parties[inParty[id][2]].msx=player(id,"tilex")
			parties[inParty[id][2]].msy=player(id,"tiley")
		elseif (tile(x,y,"frame")~=0 and parties[inParty[id][2]].mshipi~=0) then
			freeimage(parties[inParty[id][2]].mshipi)
			parties[inParty[id][2]].mshipi=0
			parse("setarmor "..id.." 0")
		end
	end
	pos[id][1]=x
	pos[id][2]=y
end

addhook("always","mshipdocked")
function mshipdocked()
	for _,id in ipairs(player(0,"table")) do
		if (inParty[id][1] and inParty[id][2]~=0 and player(id,"usgn")==parties[inParty[id][2]].mship and pstack.inMShip[id]) then
			parties[inParty[id][2]].msxx=player(id,"x")
			parties[inParty[id][2]].msyy=player(id,"y")
			for _,did in ipairs(pstack.pdocked[id]) do
				parse("setpos "..did.." "..parties[inParty[id][2]].msxx.." "..parties[inParty[id][2]].msyy)
			end
		end
	end
end